import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/model/course.dart';
import 'package:kxy_flutter_app/model/course_resource.dart';
import 'package:kxy_flutter_app/view/course/detail/course_detail_rules.dart';
import 'package:line_awesome_icons/line_awesome_icons.dart';

class CourseDetailResource extends StatelessWidget {
  final Course course;
  final Function(String) startCallback;

  const CourseDetailResource({
    Key key,
    @required this.course,
    @required this.startCallback,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> courseWidgetList = new List<Widget>();
    int index = 1;
    course.resourceList.forEach((item) {
      var resourceWidget = resourceItem(item, index++, context);
      courseWidgetList.add(resourceWidget);
    });

    var resourceListWidget = Container(
      margin: EdgeInsets.only(top: 10),
      child: Column(
        children: ListTile.divideTiles(
          tiles: courseWidgetList,
          context: context,
          color: Colors.black54,
        ).toList(),
      ),
    );

    return Container(
      color: Colors.white,
      child: Column(
        children: [
          headerWidget(context),
          resourceListWidget,
        ],
      ),
      padding: EdgeInsets.symmetric(horizontal: 15, vertical: 20),
    );
  }

  Widget headerWidget(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text('章节目录', style: TextStyle(fontSize: 18)),
        GestureDetector(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Icon(LineAwesomeIcons.question_circle, size: 16, color: Colors.black54),
              Text('完成规则', style: TextStyle(color: Colors.black54)),
            ],
          ),
          onTap: () => showRules(context),
        ),
      ],
    );
  }

  Widget resourceItem(CourseResource resource, int index, BuildContext context) {
    String leading = index < 9 ? '0$index' : index.toString();
    return ListTile(
      title: Text(resource.name, style: TextStyle(fontSize: 16)),
      leading: Text(leading, style: TextStyle(fontSize: 16, color: Colors.black54)),
      subtitle: Row(
        children: [
          resourceStatusWidget(resource, context),
          line,
          resourceTypeWidget(resource.type, context),
        ],
      ),
      trailing: actionWidget(resource, context),
    );
  }

  Widget actionWidget(CourseResource resource, BuildContext context) {
    if (resource.learning == true) {
      return Container(
        height: 30,
        width: 68,
        child: FlatButton(
          textColor: Colors.white,
          padding: EdgeInsets.all(0),
          color: Colors.black12,
          child: Text('学习中', style: TextStyle(fontSize: 12)),
          onPressed: () => {},
        ),
      );
    }

    if (resource.status == ResourceStatus.Learning) {
      return Container(
        height: 30,
        width: 68,
        child: FlatButton(
          textColor: Colors.white,
          padding: EdgeInsets.all(0),
          color: Color.fromRGBO(40, 196, 122, 1),
          child: Text('继续学习', style: TextStyle(fontSize: 12)),
          onPressed: () => startCallback(resource.id),
        ),
      );
    }

    return Container(
      height: 30,
      width: 68,
      child: FlatButton(
        textColor: Colors.white,
        padding: EdgeInsets.all(0),
        color: Theme.of(context).primaryColor,
        child: Text('开始学习', style: TextStyle(fontSize: 12)),
        onPressed: () => startCallback(resource.id),
      ),
    );
  }

  void showRules(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (cxt) => CourseDetailRules(course: course),
    );
  }

  Widget get line {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 10),
      child: Text(
        '|',
        style: TextStyle(
          color: Colors.black38,
          fontSize: 12,
        ),
      ),
    );
  }

  Widget resourceTypeWidget(ResourceType type, BuildContext context) {
    String text = '其他';
    if (type == ResourceType.Picture) {
      text = '图文';
    } else if (type == ResourceType.Video) {
      text = '视频课';
    } else if (type == ResourceType.Document) {
      text = '电子文档';
    } else if (type == ResourceType.Homework) {
      text = '作业';
    } else if (type == ResourceType.Exam) {
      text = '考试';
    }

    return Text(
      text,
      style: TextStyle(
        color: Colors.black54,
        fontSize: 12,
      ),
    );
  }

  Widget resourceStatusWidget(CourseResource resource, BuildContext context) {
    var status = resource.status;
    if (status == ResourceStatus.NotStarted) {
      return Row(
        children: [
          Icon(Icons.close, color: Colors.black54, size: 16),
          Text('未开始', style: TextStyle(color: Colors.black54, fontSize: 12)),
        ],
      );
    }
    if (status == ResourceStatus.Learning) {
      return Row(
        children: [
          Icon(Icons.timer, color: Color.fromRGBO(40, 196, 122, 1), size: 16),
          Text(
            '已开始',
            style: TextStyle(
              color: Color.fromRGBO(40, 196, 122, 1),
              fontSize: 12,
            ),
          ),
        ],
      );
    }
    return Row(
      children: [
        Icon(Icons.check, color: Theme.of(context).primaryColor, size: 14),
        Text('已完成', style: TextStyle(fontSize: 12, color: Theme.of(context).primaryColor)),
      ],
    );
  }
}
